package org.flexlancer.data
{
	import mx.core.UIComponent;
	
	import org.flexlancer.Logger.Logger;
	import org.flexlancer.components.Node;
	import org.flexlancer.datavisualization.Dashy;
	import org.flexlancer.enums.LinkTypes;
	
	public class LinkData extends UIComponent
	{
		public var btn1:Node;
		public var btn2:Node;
		public var reason:String;
		public var description:String;
		public var type:String;
		
		public function LinkData(parm1:Node,parm2:Node,parmReason:String,parmDesc:String,parmType:String)
		{
			try{
				this.btn1 = parm1;
				this.btn2 = parm2;
				this.reason = parmReason;
				this.description = parmDesc;
				this.type = parmType;
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		private function getLinkColor(parmType:String):Number{
			try{
				var color:Number = 0;
				switch(parmType){
					case LinkTypes.BELIEVED_RELIABLE:
						color = 0x00FF00;
					break;
					case LinkTypes.UNKNOWN_RELIABILITY:
						color = 0xFF6600;
					break;
					case LinkTypes.KNOWN_RELIABLE:
						color = 0x0000FF;
					break;
					case LinkTypes.BELIEVED_UNRELIABLE:
						color = 0xFFFF00;
					break;
					default:
						color = 0;
					break;					
				}
				return color;
			}catch(err:Error){
				Logger.LogE(err);
			}
			return 0x0000FF;
		}
		
		public function DrawLine():void{
			try{
				this.graphics.clear();
				while ( this.numChildren > 0 ) 
					this.removeChildAt(0);
					
				var arrLineDetails:Array = null;
				var line_color:Number = this.getLinkColor(this.type);
				
				switch(this.type){
					case LinkTypes.BELIEVED_RELIABLE:
						arrLineDetails = [8,10];
					break;
					case LinkTypes.KNOWN_RELIABLE:
						arrLineDetails = null;
					break;
					case LinkTypes.UNKNOWN_RELIABILITY:
						arrLineDetails = [16,4];
					break;
					case LinkTypes.BELIEVED_UNRELIABLE:
						arrLineDetails = [6,6];
					break;
				}
				
				if ( arrLineDetails != null ){
					var dashy:Dashy = new Dashy(5,line_color,arrLineDetails);
	
					dashy.moveTo(this.btn1.x + this.btn1.width/2, this.btn1.y + this.btn1.height/2);
					dashy.lineTo(this.btn2.x + this.btn2.width/2, this.btn2.y + this.btn2.height/2);
	
					this.addChild(dashy);				
				}else{
					this.graphics.lineStyle(5,line_color);
					this.graphics.moveTo(this.btn1.x + this.btn1.width/2, this.btn1.y + this.btn1.height/2);
					this.graphics.lineTo(this.btn2.x + this.btn2.width/2, this.btn2.y + this.btn2.height/2);
				}
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		public function getXML():XML{
			try{
				//<Link node1="101" node2="102" type="001 Believed Reliable" />
				var xmlObject:XML = new XML(<Link />);
				xmlObject.@node1 = this.btn1.name;
				xmlObject.@node2 = this.btn2.name;
				xmlObject.@type = this.type;
				xmlObject.@reason = this.reason;
				xmlObject.@description = this.description;
				
				return xmlObject;
			}catch(err:Error){
				Logger.LogE(err);
			}
			return null;
		}
	}
}